/*
    SUSAN® - Sole of Unix Save ANything

   Copyright (C) 2007-2012 Hechzh Property
      Copyright (C) 2011-2016 Skyatlas Co. LTD

   

   

   

   
*/
/*
 * extracted the TEST_PROGRAM functionality from filed/fd_plugins.cc
 * and adapted for gtest
 *
 * Andreas Rogge, Feb 2019
 */

/**
 * @file
 * Susan pluginloader
 */
#if defined(HAVE_MINGW)
#include "include/susan.h"
#include "gtest/gtest.h"
#else
#include "gtest/gtest.h"
#include "include/susan.h"
#endif

#include "filed/filed.h"
#include "filed/filed_globals.h"
#include "filed/accurate.h"
#include "filed/heartbeat.h"
#include "filed/fileset.h"
#include "filed/heartbeat.h"
#include "findlib/attribs.h"
#include "findlib/find.h"
#include "findlib/find_one.h"
#include "findlib/hardlink.h"

/**
 * Function pointers to be set here (findlib)
 */
extern int (*plugin_bopen)(SusanWinFilePacket* bfd,
                           const char* fname,
                           int flags,
                           mode_t mode);
extern int (*plugin_bclose)(SusanWinFilePacket* bfd);
extern ssize_t (*plugin_bread)(SusanWinFilePacket* bfd,
                               void* buf,
                               size_t count);
extern ssize_t (*plugin_bwrite)(SusanWinFilePacket* bfd,
                                void* buf,
                                size_t count);
extern boffset_t (*plugin_blseek)(SusanWinFilePacket* bfd,
                                  boffset_t offset,
                                  int whence);

extern char* exepath;
extern char* version;

namespace filedaemon {

int SaveFile(JobControlRecord*, FindFilesPacket*, bool);
bool SetCmdPlugin(SusanWinFilePacket*, JobControlRecord*);

/* Exported variables */
ClientResource* me; /* my resource */

int SaveFile(JobControlRecord*, FindFilesPacket*, bool) { return 0; }

bool AccurateMarkFileAsSeen(JobControlRecord*, char*) { return true; }

bool AccurateMarkAllFilesAsSeen(JobControlRecord*) { return true; }

bool accurate_unMarkFileAsSeen(JobControlRecord*, char*) { return true; }

bool accurate_unMarkAllFilesAsSeen(JobControlRecord*) { return true; }

bool SetCmdPlugin(SusanWinFilePacket*, JobControlRecord*) { return true; }

TEST(fd, fd_plugins)
{
  char plugin_dir[PATH_MAX];
  JobControlRecord mjcr1, mjcr2;
  JobControlRecord* jcr1 = &mjcr1;
  JobControlRecord* jcr2 = &mjcr2;

  InitMsg(NULL, NULL);

  OSDependentInit();

  (void)!getcwd(plugin_dir, sizeof(plugin_dir) - 1);

  LoadFdPlugins(plugin_dir, NULL);

  jcr1->JobId = 111;
  NewPlugins(jcr1);

  jcr2->JobId = 222;
  NewPlugins(jcr2);

  EXPECT_EQ(GeneratePluginEvent(jcr1, bEventJobStart, (void*)"Start Job 1"),
            bRC_OK);
  EXPECT_EQ(GeneratePluginEvent(jcr1, bEventJobEnd), bRC_OK);
  EXPECT_EQ(GeneratePluginEvent(jcr2, bEventJobStart, (void*)"Start Job 2"),
            bRC_OK);
  FreePlugins(jcr1);
  EXPECT_EQ(GeneratePluginEvent(jcr2, bEventJobEnd), bRC_OK);
  FreePlugins(jcr2);

  UnloadFdPlugins();

  TermMsg();
  CloseMemoryPool();
}
} /* namespace filedaemon */
